package com.huiyujinrong.qiangdanbao.activity.login;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.bean.ForgetBean;
import com.huiyujinrong.qiangdanbao.model.ForgetModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class ForgetPresenter implements ForgetContract.Presenter{


    @Inject
    ForgetModel model;
    private ForgetContract.View view;

    public ForgetPresenter(ForgetContract.View v){
        this.view=v;
        setupDagger2();
    }

    private void setupDagger2(){
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }


    @Override
    public void update(String phone, String smsCode, String newPwd) {
        model.update(phone,smsCode,newPwd);
    }

    @Override
    public void sendCode(String phone, int codeType) {
        model.sendCode(phone,codeType);
    }

    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEvent(RespEvent<String> event) {
        if (event.getUrl().equals(Api.rgGetCode)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.showToast("短信发送成功");
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    view.showToast("短信发送失败");
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEventForget(RespEvent<ForgetBean> event) {
        if (event.getUrl().equals(Api.forgetPwd)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.showToast("修改成功");
                    view.exit();
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    view.showToast("修改失败,请检查输入是否正确");
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }
}
